import 'package:flutter/material.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

/// Bottoast消息提醒工具类
///
///使用时需要初始化
///
/// ```dart
/// BotToastInit(
/// child:MaterialApp(
///     title: '米茫',
///     navigatorObservers: [BotToastNavigatorObserver()],//registered route observer
///     home: SplashPage(),
///   )
/// );
/// ```
class BotToastUtil {
  /// 普通文本消息
  ///
  /// [text] 需要显示的文本
  static CancelFunc showText({
    required String text,
  }) {
    return BotToast.showText(
      text: text,
    );
  }

  /// 成功消息提示
  ///
  /// [text] 提示文本
  static CancelFunc showSuccess({
    required String text,
  }) {
    return _customToastWidget(
      Icon(
        Icons.check,
        color: Colors.green,
      ),
      text,
    );
  }

  /// 错误消息提示
  ///
  /// [text] 提示文本
  static showError({
    required String text,
  }) {
    return _customToastWidget(
      Icon(
        Icons.close,
        color: Colors.red,
      ),
      text,
    );
  }

  /// 警告消息提示
  ///
  /// [text] 提示文本
  static showWarning({
    required String text,
  }) {
    return _customToastWidget(
      Icon(
        Icons.warning,
        color: Colors.yellow,
      ),
      text,
    );
  }

  ///加载中提示
  ///
  /// [loadingMsg] 自定义加载消息提示内容
  static CancelFunc showLoading({
    String? loadingMsg,
  }) {
    return _lodingWidget(loadingMsg);
  }

  /// 绘制自定义提示框
  ///
  /// [icon] 图标按钮
  ///
  /// [text] 提示文本
  static CancelFunc _customToastWidget(Icon icon, String text) {
    return BotToast.showCustomText(
      clickClose: false,
      crossPage: true,
      toastBuilder: (_) {
        return LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            return Container(
              constraints: constraints.copyWith(
                  maxWidth: constraints.biggest.width * 0.6),
              padding: EdgeInsets.only(left: 14, right: 14, top: 5, bottom: 7),
              decoration: BoxDecoration(
                color: Theme.of(context).backgroundColor,
                borderRadius: BorderRadius.all(Radius.circular(8)),
              ),
              child: Row(
                children: <Widget>[
                  icon,
                  SizedBox(
                    width: 10.0,
                  ),
                  Text(
                    text,
                    style: TextStyle(fontSize: 17, color: Colors.white),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            );
          },
        );
      },
    );
  }

  /// 绘制加载消息提示框
  static CancelFunc _lodingWidget(String? loadingMsg) {
    return BotToast.showCustomLoading(
      clickClose: false,
      allowClick: false,
      ignoreContentClick: false,
      crossPage: false,
      duration: Duration(seconds: 2),
      toastBuilder: (_) {
        return Container(
          width: 100.0,
          height: 100.0,
          child: DecoratedBox(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8.0),
            ),
            child: ClipRRect(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  SpinKitPouringHourGlass(
                    color: Color(0xFF454545),
                    size: 35.0,
                  ),
                  SizedBox(height: 12.0),
                  Container(
                    child: Text(
                      loadingMsg ?? "加载中...",
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  static cancelToast() {
    BotToast.closeAllLoading();
  }
}
